package com.guw.AWT;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}

class KeyFrame extends Frame {
    public KeyFrame() {
        setBounds(100, 200, 300, 400);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            //键盘按下方法
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘按下的键是哪一个,当前的码
                int keyCode = e.getKeyCode();
                System.out.println(keyCode);
                //根据按下键的不同,实现不同的动作
                if (keyCode == KeyEvent.VK_UP) {
                    System.out.println("你按下了上键!");
                }
            }
            //释放键盘
            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
            }
            //keyTyped指有字符被输入
            @Override
            public void keyTyped(KeyEvent e) {
                super.keyTyped(e);
            }
        });


    }
}